package com.traffic.controller;

import java.util.List;
import java.util.Date;

import com.traffic.commons.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.traffic.commons.base.BaseController;
import com.traffic.commons.utils.PageInfo;
import com.traffic.model.EcArea;
import com.traffic.service.IEcAreaService;

/**
 * <p>
 *   前端控制器
 * </p>
 * @author Hongyue Wang
 * @since 2017-04-21
 */
@Controller
@RequestMapping("/ecArea")
public class EcAreaController extends BaseController{
    
    @Autowired private IEcAreaService ecAreaService;
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/ecArea/ecAreaList";
    }


    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(EcArea ecArea, Integer page, Integer rows, String sort,String order) {
        EntityWrapper<EcArea> ew = new EntityWrapper<EcArea>();
        ew.setEntity(ecArea);
        Page<EcArea> pages = getPage(page, rows, sort, order);
        pages = ecAreaService.selectPage(pages,ew);
        return pageToPageInfo(pages);
    }
    
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/ecArea/ecAreaAdd";
    }

    @RequestMapping("/search")
    @ResponseBody
    public List<EcArea> search(String level) {
        EcArea ecArea = new EcArea();
        ecArea.setAreaLevel(Integer.valueOf(level));
        EntityWrapper<EcArea> ew = new EntityWrapper<EcArea>();
        ew.setEntity(ecArea);
        List<EcArea> ecAreas = ecAreaService.selectList(ew);
        return ecAreas;
    }

    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(EcArea ecArea) {
        ecArea.setAddTime(DateUtils.genMkDate("yy-MM-dd HH:mm:ss"));
        ecArea.setEditTime(DateUtils.genMkDate("yy-MM-dd HH:mm:ss"));
        ecArea.setIsDelete("N");
        boolean b = ecAreaService.insert(ecArea);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        EcArea ecArea = new EcArea();
        ecArea.setAreaId(id.intValue());
        ecArea.setEditTime(DateUtils.genMkDate("yy-MM-dd HH:mm:ss"));
        ecArea.setIsDelete("Y");
        boolean b = ecAreaService.updateById(ecArea);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        EcArea ecArea = ecAreaService.selectById(id);
        model.addAttribute("ecArea", ecArea);
        return "admin/ecArea/ecAreaEdit";
    }

    /**
     * 编辑
     * @param 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(EcArea ecArea) {
        ecArea.setEditTime(DateUtils.genMkDate("yy-MM-dd HH:mm:ss"));
        boolean b = ecAreaService.updateById(ecArea);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
    
}
